home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Kit PC World De Ampliacion De Windows 95
/
Kit PC World de ampliacion de Windows 95.iso
/
internet
/
sweeper
/
samples
/
progress
/
progress.cpp
< prev
next >
Wrap
C/C++ Source or Header
|
1995-12-05
|
12KB
|
339 lines
// ===========================================================================
// File: PROGRESS.CPP
//
// Copyright 1995 - 1996 Microsoft Corporation. All Rights Reserved.
// ===========================================================================
#include <urlmon.h>
#include "resource.h"
// %%Classes: ----------------------------------------------------------------
class CDownload {
public:
CDownload(LPCWSTR szURL);
HRESULT DoDownload(HWND,HWND,HWND);
LPCWSTR m_url;
private:
IMoniker* m_pmk;
IBindCtx* m_pbc;
IBindStatusCallback* m_pbsc;
};
class CBindStatusCallback : public IBindStatusCallback {
public:
// IUnknown methods
STDMETHODIMP QueryInterface(REFIID riid,void ** ppv);
STDMETHODIMP_(ULONG) AddRef() { return m_cRef++; }
STDMETHODIMP_(ULONG) Release() { if (--m_cRef == 0) { delete this; return 0; } return m_cRef; }
// IBindStatusCallback methods
STDMETHODIMP GetBindInfo(DWORD* pgrfBINDF, BINDINFO* pbindinfo);
STDMETHODIMP OnStartBinding(IBinding* pbinding);
STDMETHODIMP GetPriority(LONG* pnPriority);
STDMETHODIMP OnProgress(ULONG ulProgress, ULONG ulProgressMax, ULONG ulStatusCode,
LPCWSTR pwzStatusText);
STDMETHODIMP OnDataAvailable(DWORD grfBSCF, DWORD dwSize, FORMATETC *pFmtetc,
REFIID riid, IUnknown* punk);
STDMETHODIMP OnLowResource(DWORD dwReserved);
STDMETHODIMP OnStopBinding(HRESULT hrResult);
// constructors/destructors
CBindStatusCallback(HWND hwndStatus, HWND hwndProgress, HWND hwndText);
inline void SetStatus(LPCWSTR szStatus) { SetWndText(m_hwndStatus, szStatus); }
void SetProgress(LPCWSTR szProgress) { SetWndText(m_hwndProgress, szProgress); }
void SetWndText(HWND hwnd, LPCWSTR szText);
// data members
DWORD m_cRef;
IBinding* m_pbinding;
IStream* m_pstm;
HWND m_hwndStatus;
HWND m_hwndProgress;
HWND m_hwndText;
DWORD m_cbOld;
};
// ---------------------------------------------------------------------------
// %%Function: CBindStatusCallback::CBindStatusCallback
// ---------------------------------------------------------------------------
CBindStatusCallback::CBindStatusCallback(HWND hwndStatus, HWND hwndProgress, HWND hwndText)
{
m_hwndStatus = hwndStatus;
m_hwndProgress = hwndProgress;
m_hwndText = hwndText;
m_pbinding = NULL;
m_pstm = NULL;
m_cRef = 1;
m_cbOld = 0;
} // CBindStatusCallback
// ---------------------------------------------------------------------------
// %%Function: CBindStatusCallback::~CBindStatusCallback
// ---------------------------------------------------------------------------
CBindStatusCallback::~CBindStatusCallback()
{
if (m_pstm)
m_pstm->Release();
if (m_pbinding)
m_pbinding->Release();
} // ~CBindStatusCallback
// ---------------------------------------------------------------------------
// %%Function: CBindStatusCallback::SetWndText
// ---------------------------------------------------------------------------
inline void
CBindStatusCallback::SetWndText(HWND hwnd,LPCWSTR szText)
{
if (IsWindow(hwnd))
{
char rgchBuf[MAX_PATH];
WideCharToMultiByte(CP_ACP, 0, szText, -1, rgchBuf, MAX_PATH, 0, 0);
SetWindowText(hwnd, rgchBuf);
}
} // CBindStatusCallback::SetWndText
// ---------------------------------------------------------------------------
// %%Function: CBindStatusCallback::QueryInterface
// ---------------------------------------------------------------------------
STDMETHODIMP
CBindStatusCallback::QueryInterface(REFIID riid, void** ppv)
{
*ppv = NULL;
if (riid==IID_IUnknown || riid==IID_IBindStatusCallback)
{
*ppv = this;
AddRef();
return S_OK;
}
return E_NOINTERFACE;
} // CBindStatusCallback::QueryInterface
// ---------------------------------------------------------------------------
// %%Function: CBindStatusCallback::GetBindInfo
// ---------------------------------------------------------------------------
STDMETHODIMP
CBindStatusCallback::GetBindInfo(DWORD* pgrfBINDF, BINDINFO* pbindInfo)
{
*pgrfBINDF = BINDF_ASYNCHRONOUS;
pbindInfo->cbSize = sizeof(BINDINFO);
pbindInfo->szExtraInfo = NULL;
memset(&pbindInfo->stgmedPost, 0, sizeof(STGMEDIUM));
return S_OK;
} // CBindStatusCallback::GetBindInfo
// ---------------------------------------------------------------------------
// %%Function: CBindStatusCallback::OnStartBinding
// ---------------------------------------------------------------------------
STDMETHODIMP
CBindStatusCallback::OnStartBinding(IBinding* pbinding)
{
if (m_pbinding != NULL)
m_pbinding->Release();
m_pbinding = pbinding;
if (m_pbinding != NULL)
{
m_pbinding->AddRef();
SetStatus(L"Status: Starting to bind...");
}
return S_OK;
} // CBindStatusCallback::OnStartBinding
// ---------------------------------------------------------------------------
// %%Function: CBindStatusCallback::GetPriority
// ---------------------------------------------------------------------------
STDMETHODIMP
CBindStatusCallback::GetPriority(LONG* pnPriority)
{
return E_NOTIMPL;
} // CBindStatusCallback::GetPriority
// ---------------------------------------------------------------------------
// %%Function: CBindStatusCallback::OnProgress
// ---------------------------------------------------------------------------
STDMETHODIMP
CBindStatusCallback::OnProgress(ULONG ulProgress, ULONG ulProgressMax, ULONG ulStatusCode, LPCWSTR szStatusText)
{
char sz[255];
WideCharToMultiByte(CP_ACP, 0, szStatusText,-1, sz, 255, 0,0);
char msg[100];
WCHAR out[200];
wsprintf(msg,"Progress: %s %d of %d ", sz, ulProgress, ulProgressMax);
MultiByteToWideChar(CP_ACP, 0, msg, -1, out, sizeof(out));
SetProgress(out);
return(NOERROR);
} // CBindStatusCallback
// ---------------------------------------------------------------------------
// %%Function: CBindStatusCallback::OnDataAvailable
// ---------------------------------------------------------------------------
STDMETHODIMP
CBindStatusCallback::OnDataAvailable(DWORD grfBSCF, DWORD dwSize, FORMATETC *pFmtetc, REFIID riid, IUnknown *punk)
{
// Assert(riid == IID_IStream);
if (!m_pstm)
m_pstm = (IStream *)punk;
if (dwSize > m_cbOld)
{
DWORD dwRead = dwSize - m_cbOld;
DWORD dwActuallyRead;
char * lp = new char[dwRead];
m_pstm->Read(lp,dwRead,&dwActuallyRead);
SetWindowText(m_hwndText,lp);
delete lp;
m_cbOld = dwSize;
}
return S_OK;
} // CBindStatusCallback::OnDataAvailable
// ---------------------------------------------------------------------------
// %%Function: CBindStatusCallback::OnLowResource
// ---------------------------------------------------------------------------
STDMETHODIMP
CBindStatusCallback::OnLowResource(DWORD dwReserved)
{
return E_NOTIMPL;
} // CBindStatusCallback::OnLoadResource
// ---------------------------------------------------------------------------
// %%Function: CBindStatusCallback::OnStopBinding
// ---------------------------------------------------------------------------
STDMETHODIMP
CBindStatusCallback::OnStopBinding(HRESULT hrStatus)
{
SetStatus(L"Status: Done.");
return S_OK;
} // CBindStatusCallback::OnStopBinding
// ---------------------------------------------------------------------------
// %%Function: CDownload::CDownload
// ---------------------------------------------------------------------------
CDownload::CDownload(LPCWSTR szURL)
{
m_url = szURL;
m_pmk = 0;
m_pbc = 0;
m_pbsc = 0;
} // CDownload
// ---------------------------------------------------------------------------
// %%Function: CDownload::~CDownload
// ---------------------------------------------------------------------------
CDownload::~CDownload()
{
if (m_pmk)
m_pmk->Release();
if (m_pbc)
m_pbc->Release();
if (m_pbsc)
m_pbsc->Release();
} // ~CDownload
// ---------------------------------------------------------------------------
// %%Function: CDownload::DoDownload
// ---------------------------------------------------------------------------
HRESULT
CDownload::DoDownload(HWND hwndStatus, HWND hwndProgress, HWND hwndText)
{
HRESULT hr = CreateURLMoniker(NULL, m_url, &m_pmk);
if( SUCCEEDED(hr) )
{
m_pbsc = new CBindStatusCallback(hwndStatus, hwndProgress, hwndText);
if (m_pbsc != NULL)
m_pbsc->AddRef();
else
hr = E_OUTOFMEMORY;
}
if (SUCCEEDED(hr))
hr = CreateBindCtx(0, &m_pbc);
if (SUCCEEDED(hr))
hr = m_pbc->RegisterObjectParam(OLESTR("BindStatusCallback"), m_pbsc);
IStream* pstm;
if (SUCCEEDED(hr))
hr = m_pmk->BindToStorage(m_pbc, 0, IID_IStream, (void**)&pstm);
return hr;
} // CDownload::DoDownload
// ---------------------------------------------------------------------------
// %%Function: DialogProc
// ---------------------------------------------------------------------------
BOOL CALLBACK
DialogProc(HWND hwndDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
static CDownload* pcdl = NULL;
switch(message)
{
case WM_INITDIALOG:
EnableWindow(GetDlgItem(hwndDlg, IDOK), TRUE);
EnableWindow(GetDlgItem(hwndDlg, IDCANCEL), TRUE);
pcdl = reinterpret_cast<CDownload*>(lParam);
break;
case WM_COMMAND:
{
switch (LOWORD(wParam))
{
case IDOK:
{
char rgchBuf[MAX_PATH];
HWND hwndStatus = GetDlgItem(hwndDlg, IDC_DISPLAY);
HWND hwndProgress = GetDlgItem(hwndDlg, IDC_PROGRESS);
HWND hwndText = GetDlgItem(hwndDlg, IDC_TEXT);
EnableWindow(GetDlgItem(hwndDlg, IDOK), FALSE);
EnableWindow(GetDlgItem(hwndDlg, IDCANCEL), FALSE);
WideCharToMultiByte(CP_ACP, 0, pcdl->m_url, -1, rgchBuf, MAX_PATH, 0, 0);
SetWindowText(hwndDlg, rgchBuf);
SetWindowText(hwndStatus, TEXT("Status: Inititating Bind..."));
pcdl->DoDownload(hwndStatus, hwndProgress, hwndText);
EnableWindow(GetDlgItem(hwndDlg, IDCANCEL), TRUE);
break;
}
case IDCANCEL:
EndDialog(hwndDlg,0);
return 1;
}
break;
}
}
return 0;
} // DialogProc
// ---------------------------------------------------------------------------
// %%Function: WinMain
// ---------------------------------------------------------------------------
int WINAPI
WinMain(HINSTANCE hinst, HINSTANCE hinstPrev, LPSTR szCmdLine, int nCmdShow)
{
static WCHAR szDefaultURL[] = L"http://www.home.msn.com";
WCHAR rgwchCmdLine[MAX_PATH];
MultiByteToWideChar(CP_ACP, 0, szCmdLine, -1, rgwchCmdLine, MAX_PATH);
CDownload download(rgwchCmdLine[0] ? rgwchCmdLine : szDefaultURL);
DialogBoxParam(hinst, MAKEINTRESOURCE(IDD_DIALOG1), HWND_DESKTOP,
(FARPROC)&DialogProc, reinterpret_cast<LPARAM>(&download));
return 0;
} // WinMain
// EOF =======================================================================